package com.www.applyword.ui.my;

import android.text.Editable;
import android.text.Selection;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import com.www.applyword.R;
import com.www.applyword.base.BaseActivity;
import com.www.applyword.utils.Utils;
import com.www.applyword.view.CustomLoadingDialog;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * author : huang
 * date   : 2020/8/1 0001 9:57
 * version: 1.0
 */
public class OpintionActivity extends BaseActivity {
    @BindView(R.id.et)
    EditText et;
    @BindView(R.id.tv)
    TextView tv;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.opintion_activity);
    }

    @Override
    protected void loadData() {
        et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Editable editable = et.getText();
                int len = editable.length();
                //显示还可以输入多少字
                tv.setText(len + "" + "/200");
                if (len > 200) {
                    Utils.showToastSafe("超出字数限制");
                    int selEndIndex = Selection.getSelectionEnd(editable);
                    String str = editable.toString();
                    //截取新字符串
                    String newStr = str.substring(0, 500);
                    et.setText(newStr);
                    editable = et.getText();

                    //新字符串的长度
                    int newLen = editable.length();
                    //旧光标位置超过字符串长度
                    if (selEndIndex > newLen) {
                        selEndIndex = editable.length();
                    }
                    //设置新光标所在的位置
                    Selection.setSelection(editable, selEndIndex);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @OnClick(R.id.sb_submit)
    public void onViewClicked() {
        if (TextUtils.isEmpty(et.getText().toString().trim()) ) {
            Utils.showToastSafe("请先添加内容");
            return;
        }
        CustomLoadingDialog.show(this);
        final Timer t = new Timer();
        t.schedule(new TimerTask() {
            @Override
            public void run() {
                CustomLoadingDialog.dismiss(OpintionActivity.this);
                Utils.showToastSafe("意见反馈成功");
                t.cancel();
                finish();
            }
        }, 2000);
    }
}
